/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月16日
 */
package com.massyframework.beanskin.web.utils;

import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.framework.Framework;
import com.massyframework.beanskin.service.ServiceReference;

/**
 * 提供{@link #retrieveFraemwork(ServletContext)}方法，取回Framework实例
 * @author huangkh
 *
 */
public class WebUtils {
	
	/**
     * 尝试从ServletContext中取回{@link Framework}
     * @param servletContext {@link ServletContext}, Servlet上下文
     * @return {@link Framework}
	 * @throws ServletException 如果Framework不存在，则抛出例外
     */
    public static Framework retrieveFraemwork(ServletContext servletContext) throws ServletException{
        Object result = servletContext.getAttribute(Framework.class.getName());
        if (result != null){
            if (Framework.class.isAssignableFrom(result.getClass())){
                return Framework.class.cast(result);
            }
        }

        throw new ServletException("cannot found Framework with ServletContext.");
    }
    
    /**
     * 从{@code config}中取回组编号
     * @param config {@link ServletConfig},servlet配置
     * @return {@link String}
     * @throws ServletException 如果未设置符号名称，则抛出例外
     */
    public static String retrieveGroupId(ServletConfig config) throws ServletException {    	
        String result = config.getInitParameter(ServiceReference.MODULE_GROUPID);
        if (result == null) {
            throw new ServletException("cannot found parameter by servletConfig:" +
                    " name=\"" + ServiceReference.MODULE_GROUPID + "\".");
        }
        return result;
    }
    
    /**
     * 从{@code config}中取回模块编号
     * @param config {@link ServletConfig},servlet配置
     * @return {@link String}
     * @throws ServletException 如果未设置符号名称，则抛出例外
     */
    public static String retrieveModuleId(ServletConfig config) throws ServletException {
    	String result = config.getInitParameter(ServiceReference.MODULE_MODULEID);
        if (result == null) {
            throw new ServletException("cannot found parameter by servletConfig:" +
                    " name=\"" + ServiceReference.MODULE_MODULEID + "\".");
        }
        return result;
    }
    
    /**
     * 从{@code config}中取回模块版本
     * @param config {@link ServletConfig},servlet配置
     * @return {@link String}
     * @throws ServletException 如果未设置符号版本，则抛出例外
     */
    public static String retrieveVersion(ServletConfig config) throws ServletException{
    	 String result = config.getInitParameter(ServiceReference.MODULE_VERSION);
         if (result == null) {
             throw new ServletException("cannot found parameter by servletConfig:" +
                     " name=\"" + ServiceReference.MODULE_VERSION + "\".");
         }
         return result;
    }

    /**
     * 从{@code config}中取回关联的{@link ServiceExportableModule}
     * @param config {@link ServletConfig}
     * @return {@link ServiceExportableModule}
     */
    public static final Module<?> retrieveAssociateModule(ServletConfig config) throws ServletException{
        Framework framework = retrieveFraemwork(config.getServletContext());
        String groupId = retrieveGroupId(config);
        String moduleId = retrieveModuleId(config);
        String version = retrieveVersion(config);
        
        Module<?> result = framework.findModuleByIdentifier(groupId, moduleId, version);
        if (result == null) {
        	throw new ServletException("cannot found ServiceExportableModule: groupId=" + groupId + ",moduleId=" + moduleId + ", version=" + version);
        }
        
        return result;
    }
    
    /**
     * 从{@code config}中取回组编号
     * @param config {@link FilterConfig},过滤器配置
     * @return {@link String}
     * @throws ServletException 如果未设置符号名称，则抛出例外
     */
    public static String retrieveGroupId(FilterConfig config) throws ServletException {
        String result = config.getInitParameter(ServiceReference.MODULE_GROUPID);
        if (result == null) {
            throw new ServletException("cannot found parameter by servletConfig:" +
                    " name=\"" + ServiceReference.MODULE_GROUPID + "\".");
        }
        return result;
    }

    /**
     * 从{@code config}中取回模块编号
     * @param config {@link FilterConfig},过滤器配置
     * @return {@link String}
     * @throws ServletException 如果未设置符号名称，则抛出例外
     */
    public static String retrieveModuleId(FilterConfig config) throws ServletException {
        String result = config.getInitParameter(ServiceReference.MODULE_MODULEID);
        if (result == null) {
            throw new ServletException("cannot found parameter by servletConfig:" +
                    " name=\"" + ServiceReference.MODULE_MODULEID + "\".");
        }
        return result;
    }
    
    /**
     * 从{@code config}中取回模块版本
     * @param config {@link FilterConfig},过滤器配置
     * @return {@link String}
     * @throws ServletException 如果未设置符号名称，则抛出例外
     */
    public static String retrieveVersion(FilterConfig config) throws ServletException {
        String result = config.getInitParameter(ServiceReference.MODULE_VERSION);
        if (result == null) {
            throw new ServletException("cannot found parameter by filterConfig:" +
                    " name=\"" + ServiceReference.MODULE_VERSION + "\".");
        }
        return result;
    }

    /**
     * 从{@code config}中取回关联的{@link ServiceExportableModule}
     * @param config {@link ServletConfig}
     * @return {@link ServiceExportableModule}
     */
    public static final Module<?> retrieveAssociateModule(FilterConfig config) throws ServletException{
        Framework framework = retrieveFraemwork(config.getServletContext());
        String groupId = retrieveGroupId(config);
        String moduleId = retrieveModuleId(config);
        String version = retrieveVersion(config);
        
        Module<?> result = framework.findModuleByIdentifier(groupId, moduleId, version);
        if (result == null) {
        	throw new ServletException("cannot found ServiceExportableModule: groupId=" + groupId + ",moduleId=" + moduleId + ", version=" + version);
        }
        
        
        return result;
    }
}
